package cn.azzhu.day07

import cn.azzhu.day02.SensorSource
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api._
import org.apache.flink.api.scala._
import org.apache.flink.table.api.bridge.scala.{StreamTableEnvironment, tableConversions}
import org.apache.flink.types.Row
/**
 * @author azzhu
 * @create 2020-09-23 21:57:53
 */
object SensorReadingCountBySQL {
  def main(args: Array[String]): Unit = {
    val env = StreamExecutionEnvironment.getExecutionEnvironment
    env.setParallelism(1)

    val settings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner()
      .inStreamingMode()
      .build()

    val tEnv = StreamTableEnvironment.create(env, settings)

    val stream = env.addSource(new SensorSource)
    tEnv.createTemporaryView("sensor",stream,$"id",$"timestamp",$"temperature")

    tEnv.sqlQuery("select id,count(id) from sensor group by id")
      .toRetractStream[Row]
      .print()

    env.execute("SensorReadingCountBySQL")
  }
}
